package com.wardrobe.soa.api.userinfo;

import com.wardrobe.dao.address.AddressDao;
import com.wardrobe.dao.userinfo.UserInfoDao;
import com.wardrobe.domain.address.AddressBean;
import com.wardrobe.domain.user.UserInfo;
import com.wardrobe.domain.userinfo.UserInfoBean;
import com.wardrobe.domain.userinfo.UserInfoShowBean;
import com.wardrobe.soa.api.user.UserDao;
import com.wardrobe.soa.common.ApiResult;
import com.wardrobe.soa.utils.CommonConsant;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by Administrator on 2018/11/14.
 */
@Service
public class ApiUserInfoService {
    private static final Logger logger = LoggerFactory.getLogger(ApiUserInfoService.class);
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private AddressDao addressDao;
    public ApiResult getUserInfo(long userId) {
        try {
            UserInfoShowBean userInfoShowBean = userInfoDao.selectUserInfo(userId);
            AddressBean addressBean = addressDao.selectDefaultAddress(userId);
            if (addressBean!=null){
                userInfoShowBean.setAddress(addressBean.getAddress());
            }
            return new ApiResult(CommonConsant.successCode, CommonConsant.success, CommonConsant.successMsg, userInfoShowBean);
        } catch (Exception e) {
            logger.error("getUserInfo error", e);
        }
        return new ApiResult(CommonConsant.serverFailedCode, CommonConsant.failed, CommonConsant.serverFailedMsg, "");
    }

    public ApiResult modifyUserInfoById(UserInfoBean bean) {
        try {
            bean.setUpdateTime(new Date());
            int flag = userInfoDao.updateUserInfoById(bean);
            if (flag == 1) {
                return new ApiResult(CommonConsant.successCode, CommonConsant.success, CommonConsant.successMsg, flag);
            }
        } catch (Exception e) {
            logger.error("modifyUserInfoById error", e);
        }
        return new ApiResult(CommonConsant.serverFailedCode, CommonConsant.failed, CommonConsant.serverFailedMsg, "");
    }

    public ApiResult saveUserInfo() {
        try {
            UserInfo info = new UserInfo();
            info.setMobileNumber("15515895654");
            String encodedPassword = new Sha512Hash("123456").toBase64();
            info.setPassword(encodedPassword);
            int flag = userDao.insertUser(info);
            if (flag == 1) {
                UserInfoBean infoBean= new UserInfoBean();
                infoBean.setUserId(info.getId());
                userInfoDao.insertUserInfo(infoBean);
                return new ApiResult(CommonConsant.successCode, CommonConsant.success, CommonConsant.successMsg, flag);
            }
        } catch (Exception e) {
            logger.error("saveUserInfo error", e);
        }
        return new ApiResult(CommonConsant.serverFailedCode, CommonConsant.failed, CommonConsant.serverFailedMsg, "");
    }
}
